package com.tyut.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.tyut.mapper.AdminMapper;
import com.tyut.mapper.ArticleMapper;
import com.tyut.mapper.EditorMapper;
import com.tyut.pojo.*;
import com.tyut.service.EditorService;
import com.tyut.utils.Md5Util;
import com.tyut.utils.ThreadLocalUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class EditorServiceImpl implements EditorService {
	@Autowired
	private EditorMapper editorMapper;
	@Autowired
	private AdminMapper adminMapper;
	@Autowired
	private ArticleMapper articleMapper;
	@Override
	public PageBean<Article> list(Integer pageNum, Integer pageSize, Integer categoryId,String state, String checkState) {
		//2.开启分页查询，需要使用PageHelper插件，需要导入这个插件的坐标
		//PageHHelper会自动将pageNum和pageSize拼接到SQL语句后面，加上limit，完成一个分页查询
		PageHelper.startPage(pageNum,pageSize);
		Map<String,Object> map = ThreadLocalUtil.get();
		String userRole = (String) map.get("userRole");
		if (userRole.equals("2")){
			Page<Article> page  = adminMapper.list(categoryId,state,checkState);
			return  new PageBean(page.getTotal(),page.getResult());
		}
		if(userRole.equals("0")){
			Page<Article> page  = articleMapper.authorlist(categoryId,state,checkState);
			return  new PageBean(page.getTotal(),page.getResult());
		}
		categoryId = (Integer) map.get("id");
		//3.调用mapper
		Page<Article> page  = editorMapper.list(categoryId, state, checkState);


		return new PageBean(page.getTotal(),page.getResult());
	}

	@Override
	public List<Category> findAll() {
		List<Category> category = editorMapper.find();
		return category;
	}

	@Override
	public User findByName(String username) {
		User user = editorMapper.findByName(username);
		return user;
	}

	@Override
	public void updateAvatar(String avatarUrl) {
		Editor editor = new Editor();
		Map<String,Object> map = ThreadLocalUtil.get();
		Integer id = (Integer) map.get("id");
		editor.setId(id);
		editor.setUserPic(avatarUrl);
		editor.setUpdateTime(LocalDateTime.now());
		editorMapper.updateAvatar(editor);
	}

	@Override
	public void update(User user) {
		Editor editor = new Editor();
		BeanUtils.copyProperties(user,editor);
		editor.setUpdateTime(LocalDateTime.now());
		editorMapper.update(editor);
	}

	@Override
	public void updatePwd(String newPwd) {
		Map<String, Object> map = ThreadLocalUtil.get();
		Editor editor = new Editor();
		editor.setId((Integer) map.get("id"));
		String md5String = Md5Util.getMD5String(newPwd);
		editor.setPassword(md5String);
		editor.setUpdateTime(LocalDateTime.now());
		editorMapper.updatePwd(editor);
	}

	@Override
	public PageBean<Editor> editorlist(Integer pageNum, Integer pageSize, String username) {
		PageHelper.startPage(pageNum,pageSize);
		Page<Editor> page = editorMapper.editorlist(username);
		return new PageBean(page.getTotal(),page.getResult());
	}

	@Override
	public void updateState(Integer id, String state) {
		editorMapper.updateState(id,state);
	}

	@Override
	public void delete(Integer id) {
		editorMapper.delete(id);
	}
}
